package com.zzh.partnersys.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.StringUtils;

/**
 * @author zzh
 * @version 1.0
 * RedissonConfig
 */
@Configuration
@ConfigurationProperties(prefix = "spring.data.redis")
@Data
public class RedissonConfig {

    private String host;
    private Integer port;
    private String password;

    @Bean
    @Primary
    public RedissonClient redissonClient(){
        Config config = new Config();
        String redisAddress = String.format("redis://%s:%d", host, port);
        
        // 构建单服务器配置
        var singleServerConfig = config.useSingleServer()
                .setAddress(redisAddress)
                .setDatabase(3);
        
        // 只有当密码不为空时才设置密码
        if (StringUtils.hasText(password)) {
            singleServerConfig.setPassword(password);
        }
        
        return Redisson.create(config);
    }
}
